package com.lans.linear;

import java.util.Iterator;

/**
 * @author: lans
 * @date: 2025/2/19
 * @name: 刘宇
 */
public class TwoWayLinkList<T> implements Iterable<T> {
    @Override
    public Iterator<T> iterator() {
        return new MyIterator();
    }
    private class MyIterator implements Iterator<T>{

        private Node node = head;
        @Override
        public boolean hasNext() {
            return node.next!=null;
        }

        @Override
        public T next() {
            node = node.next;
            return node.item;
        }
    }

    public class Node{
        public T item;
        public Node pre;
        public Node next;

        public Node(T item, Node pre, Node next) {
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
    }
    public TwoWayLinkList(){
        head = new Node(null,null,null);
        last = null;
        N=0;
    }
    //头结点
    private Node head;
    //尾节点
    private Node last;
    //链表长度
    private int N;

    public void clear(){
        last = null;
        head.next = last;
        head.pre = null;
        head.item = null;
        N=0;
    }

    public boolean isEmpty(){
        return N==0;
    }

    public int length(){
        return N;
    }

    public T get(int i){
        if(i<0 || i>=N) {
            throw new RuntimeException("该位置不和法");
        }
        Node temp = head.next;
        for(int index=0;index<i;index++) {
            temp = temp.next;
        }
        return temp.item;
    }

    public void insert(T t){
        if(last ==null){
            last =  new Node(t,head,null);
            head.next = last;
        }else{
            Node oldLast = last;
            Node newLast = new Node(t, oldLast, null);
            oldLast.next = newLast;
            last = newLast;
        }
        N++;
    }

    public void insert(int i,T t){
        if(i<0 || i>=N) {
            throw new RuntimeException("位置不合理");
        }
        Node temp = head;
        for(int index = 0;index<i;index++){
            temp = temp.next;
        }
        Node current = temp.next;
        Node node = new Node(t,temp,current);
        current.pre = node;
        temp.next = node;
        N++;
    }

    public T remove(int i){
        if(i<0 || i>=N) {
            throw new RuntimeException("位置不合理");
        }
        Node temp = head;
        for(int index = 0;index<i;index++){
            temp = temp.next;
        }
        Node current = temp.next;
        //获取要删除节点的下一个节点
        Node node = current.next;
        temp.next = node;
        node.pre = temp;
        N--;
        return current.item;
    }

    //
    public int indexOf(T t){
        Node temp = head;
        for(int index = 0;temp.next!=null;index++){
            temp = temp.next;
            if(temp.next.equals(t)){
                return index;
            }
        }
        return -1;
    }

    //获取头节点
    public T getFirst(){
        if(isEmpty()){
            return null;
        }
        return head.next.item;
    }

    //获取尾节点/
    public T getLast(){
        if(isEmpty()){
            return null;
        }
        return last.item;
    }


    public static void main(String[] args) {
        TwoWayLinkList<String> list = new TwoWayLinkList<String>();
        list.insert("孙悟空");
        list.insert("虞姬");
        list.insert("貂蝉");
        list.insert("西施");

        list.insert(2,"元歌");
//        System.out.println(list.remove(3));
//        System.out.println(list.getLast());
//        System.out.println(list.getFirst());
//        for(String str:list){
//            System.out.println(str);
//        }
        System.out.println("===================");
        list.reverse();
        for(String str:list){
            System.out.println(str);
        }
    }
    public void reverse(){
        if(N!=0){
            reverse(head.next);
        }
    }
    public Node reverse(Node current){

        if(current.next==null){
            head.next = current;
            return current;
        }
        Node pre = reverse(current.next);
        pre.next = current;
        current.next = null;
        return current;
    }

    @Override
    public String toString() {

        return super.toString();
    }
}